package com.woniuxy.huayoung.controller;

import com.woniuxy.huayoung.entity.Order;
import com.woniuxy.huayoung.entity.TravelGoods;
import com.woniuxy.huayoung.result.ResponseResult;
import com.woniuxy.huayoung.service.TravelGoodsOrderService;
import com.woniuxy.huayoung.service.TravelGoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/travelgoods")
public class TravelGoodsController {
    private final Logger LOGGER= LoggerFactory.getLogger(TravelGoodsController.class);

    @Resource
    private TravelGoodsService travelGoodsService;

    @Resource
    private TravelGoodsOrderService travelGoodsOrderService;

    //自由行页面根据出发地和目的地搜索旅游商品
    @RequestMapping("/search")
    public ResponseResult<List<TravelGoods>> search(TravelGoods travelGoods){

        Date departureTime_start = travelGoods.getDepartureTime_start();
        Date departureTime_end = travelGoods.getDepartureTime_end();


        List<TravelGoods> travelGoodsList = null;
        //查看是否接收到前端数据
        LOGGER.debug(travelGoods.getCategory() + travelGoods.getFrom()+travelGoods.getTo());

        if(travelGoods.getPrice() != null && departureTime_start != null && departureTime_end != null){
            //根据出发地/目的地/商品类别/旅游日期/商品价格搜索旅游商品
            if(travelGoods.getPrice().equals(new BigDecimal(6000))){
                //根据返回的价格大于6000元
                travelGoodsList = travelGoodsService.findByTravelGoods3(travelGoods);
            } else{
                //根据返回的价格小于于6000元
                travelGoodsList = travelGoodsService.findByTravelGoods2(travelGoods);
            }
        } else if(travelGoods.getPrice() == null && departureTime_start != null && departureTime_end != null){
            //根据出发地/目的地/商品类别/旅游日期搜索旅游商品
            travelGoodsList = travelGoodsService.findByTravelGoods1(travelGoods);

        } else if(travelGoods.getPrice() == null && departureTime_start == null && departureTime_end == null){
        //根据出发地/目的地/商品类别搜索旅游商品
                travelGoodsList = travelGoodsService.findByTravelGoods(travelGoods);
        }


        //输出查询到的数据
        LOGGER.debug(travelGoodsList.toString());

        //根据从数据库返回的数据判断应该返回给前端的结果
        if(travelGoodsList != null){
            LOGGER.debug("搜索旅游数据成功");
            //将数据集合进行封装返回
            return new ResponseResult<List<TravelGoods>>(200,"success",travelGoodsList);
        } else {
            LOGGER.debug("搜索旅游数据失败");
            return new ResponseResult<List<TravelGoods>>(400,"fail",travelGoodsList);
        }
    }

    //点击商品进入详情页面
    @RequestMapping("/detail")
    public ResponseResult<TravelGoods> goodsDetail(int id){
        //测试是否接收到参数
//        log.debug("id:" + id);

        //根据id查询数据库中的响应的商品详情
        TravelGoods travelGoods = travelGoodsService.findGoodsById(id);

        //输出查询到的数据
        LOGGER.debug(travelGoods.toString());

        //根据从数据库返回的数据判断应该返回给前端的结果
        if(travelGoods != null){
            LOGGER.debug("搜索商品详情数据成功");
            //将数据集合进行封装返回
            return new ResponseResult<TravelGoods>(200,"success",travelGoods);
        } else {
            LOGGER.debug("搜索商品详情数据失败");
            return new ResponseResult<TravelGoods>(400,"fail",travelGoods);
        }
    }

    //点击加减号键来增加减少购买商品数量,前端传回的参数是商品id和将要购买的商品数量
    @RequestMapping("/choosegoodsnum")
    public ResponseResult<String> choosegoodsnum(Integer id, Integer goodsnum){
        LOGGER.debug("用户点击加减号选择商品数量");

        //根据前端传回参数更改redis缓存中的数据
        Boolean isupdate = travelGoodsService.choosegoodsnum(id, goodsnum);
        LOGGER.debug("前端传回参数更改redis缓存中数据成功");

        //根据从数据库返回的数据判断应该返回给前端的结果
        if(! isupdate){
            LOGGER.debug("库存量不足");
            //将数据集合进行封装返回
            String data = "库存量不足";
            return new ResponseResult<String>(400,"success",data);
        } else {
            LOGGER.debug("库存量充足");
            String data = "库存量充足";
            return new ResponseResult<String>(200,"fail",data);
        }
    }

    //点击购买跳转到订单列表(前端传回商品id和商品数量num)
    @RequestMapping("/order")
    public ResponseResult<Order> orderlist(Order order){
        //日志测试
//        log.debug("goodsid:"+order.getGoodsid()+"..."+"num"+order.getNum());

        //根据商品id和商品数量调用生成订单方法并返回订单号
        String orderno = travelGoodsOrderService.addOrder(order);
        LOGGER.debug("添加订单到数据库成功");


        //根据orderno订单号查询订单信息
        Order neworder = travelGoodsOrderService.findByOrderNo(orderno);
        LOGGER.debug("返回订单信息到前端订单列表页面成功");

        //根据从数据库返回的数据判断应该返回给前端的结果
        if(neworder != null){
            LOGGER.debug("搜索商品详情数据成功");
            //将数据集合进行封装返回
            return new ResponseResult<Order>(200,"success",neworder);
        } else {
            LOGGER.debug("搜索商品详情数据失败");
            return new ResponseResult<Order>(400,"fail",neworder);
        }
    }

    //用户点击支付按钮前端传回订单号(orderno)根据支付订单的状态跳转到相应页面同时也要返回相应的订单信息
    @RequestMapping("/pay")
    public ResponseResult<Order> paycheck(String orderno){

        //根据订单号查询订单信息进行审核并返回审核状态
        Integer payCheckStatus = travelGoodsOrderService.payCheck(orderno);

        //获取到该订单内容并返回前端
        Order order = travelGoodsOrderService.findByOrderNo(orderno);

        //根据从数据库返回的数据判断应该返回给前端的结果
        if(payCheckStatus.equals(3)){
            LOGGER.debug("商品已下架");
            //将数据集合进行封装返回
            return new ResponseResult<Order>(400,"fail",order);
        } else if(payCheckStatus.equals(2)){
            LOGGER.debug("该商品库存量不足");
            return new ResponseResult<Order>(400,"fail",order);
        }  else {
            LOGGER.debug("该订单审核成功可以进行支付");
            return new ResponseResult<Order>(200,"success",order);
        }
    }
}
